// pages/orders/orders.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: [{
        name: "全部"
      },
      {
        name: "待付款"
      },
      {
        name: "待发货"
      },
      {
        name: "待收货"
      },
      {
        name: "已完成"
      },

    ], //选项卡
    current: 0,
    index: 0,
    shoplist: ''
  },
  // 切换
  chooseTab: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.i
    let t = this,
      i = e.target.dataset.i;
    t.setData({
      current: i,
      index: index
    })
    this.getAllshoporders(index)
  },
  // 删除
  delete: function (e) {
    console.log(e)
    var shoplist = this.data.list
    let index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id
    shoplist.splice(index, 1)
    this.Delteorderss(id)
    this.setData({
      list: shoplist,
    })
    console.log(this.data.list)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
  },
  chakwliu: function (e) {
    var that = this
    var orderunique = e.currentTarget.dataset.orderunique
    var id = e.currentTarget.dataset.id
    var orderss = e.currentTarget.dataset.orderss
    var orderstatus = e.currentTarget.dataset.orderstatus
    //查询默认物流公司
    app.util.request({
      'url': 'entry/wxapp/index.wuliucheack',
      data: {},
      success: function (res) {
        console.log(res)
        var name = res.data.name
        var com = res.data.com
        wx.navigateTo({
          url: '/hyb_yl/userCommunicate/pages/load/load?gsname=' + name + '&orderss=' + orderss + '&id=' + id + '&com=' + com + '&num=' + orderunique + '&orderstatus=' + orderstatus,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    that.getAllshoporders();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getAllshoporders() {
    var that = this
    var openid = wx.getStorageSync('openid')
    var index = that.data.index
    app.util.request({
      'url': 'entry/wxapp/chufang.doPagecflist',
      data: {
        index: index,
        openid: openid,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          list: res.data
        })
      }
    })
  },
  Delteorderss: function (id) {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/goods.delete',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
  //支付
  zhifu: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var orders = e.currentTarget.dataset.orders
    var money = e.currentTarget.dataset.money
    var tkid = app.globalData.tkid //一级
    var mytkid = app.globalData.mytkid //二级
    app.util.request({
      'url': 'entry/wxapp/yuyue.paycforder',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orders: orders
      },
      success: function (res) {
        console.log(res)
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {
            console.log(res)
            app.util.request({
              'url': 'entry/wxapp/tuike.uptopmoney',
              data: {
                tkid: tkid,
                mytkid: mytkid,
                money: money,
                openid: wx.getStorageSync('openid'),
                orders: orders,
                leixing: 'yuanchengkaifang'
              },
              success: function (res) {
                console.log(res)
              }
            })
            that.getAllshoporders()
          }
        })
      }
    })

  },
  enter: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var totalMoney = e.currentTarget.dataset.totalMoney
    wx.showModal({
      content: '是否确认收货',
      success: function (resa) {
        if (resa.cancel) {
          console.log('用户点击了取消')
        } else {
          //点击确定
          app.util.request({
            'url': 'entry/wxapp/goods.upstatus',
            data: {
              id: id,
              ordersStatus: 2
            },
            success: function (res) {
              wx.showToast({
                title: '您已收货',
                icon: 'none',
                duration: 3000
              })
              that.getAllshoporders()
            }
          })
        }
      }

    })

  },
  goumai: function (e) {
    var that = this
    var sid = e.currentTarget.dataset.sid
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/product/product?sid=' + sid,
    })
  },
  ordersinfo: function (e) {
    var that = this
    var sid = e.currentTarget.dataset.sid
    var id = e.currentTarget.dataset.id
    var orderStatus = e.currentTarget.dataset.orderStatus
    var xufang = 0
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/sure_Details/sure_Details?id=' + id + '&orderStatus=' + orderStatus + '&xufang=' + xufang
    })
  },
  pingjia: function (e) {
    var that = this
    console.log(e)
    var sid = e.currentTarget.dataset.sid
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/addComment/addComment?sid=' + sid + '&id=' + id,
    })
  },
  //提醒发货
  tixing() {
    var that = this

  }
})